<template>
  <div class="pagecard">
    <el-card class="card">
      <el-button type="primary" round @click="addPermissionList('0','1')">添加权限</el-button>
    </el-card>

    <el-card style="margin:20px 0 0 0">
      <el-table :data="permissionList" border row-key="id" default-expand-all>
        <!-- <el-table-column label="序号" prop="index" /> -->
        <el-table-column label="权限名称" prop="title" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="权限描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button v-if="scope.row.type==='1'" type="primary" @click="addPermissionList(scope.row.id,'2')">添加子权限</el-button>
            <el-button v-if="scope.row.type!=='1'" style="visibility:hidden" type="primary" @click="addPermissionList(scope.row.id,'2')">添加子权限</el-button>
            <el-button @click="editPermissionList(scope.row.id)">编辑</el-button>
            <el-button type="danger" @click="delPermissionList(scope.row.id)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- <el-pagination
        class="page"
        layout="prev, pager, next"
        :total="total"
      /> -->
    </el-card>
    <!-- 添加弹窗 -->
    <el-dialog :title="formData.id?'编辑权限':'添加权限'" :visible="isShowDialog" @close="btnCancel">
      <el-form>
        <el-form-item label="权限名称">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <!-- 确认取消按钮 -->
      <template #footer>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermission, getPermissionPage, addPermission, delPermission, getPermissionId, editPermission } from '@/api/permission'
// 引入数据转树形结构
import { treeList } from '@/utils/index'

export default {
  data() {
    return {
      permissionList: [],
      total: 0,
      page: 1,
      // pagination: {
      //   _limit: 100,
      //   _start: 0
      // },
      isShowDialog: false,
      formData: {
        title: '',
        code: '',
        description: ''
      }
    }
  },
  created() {
    this.loadList()
  },
  methods: {
    async loadList() {
      const res = await getPermission()
      this.permissionList = treeList(res, '0')
      // console.log('原数据', res)
      // 索引
      // this.permissionList = treeList(res, '0').map((item, index) => {
      //   return {
      //     ...item,
      //     index: (this.page - 1) * (this.pagination._limit) + index + 1
      //   }
      // })
      // console.log('转换后新数据', this.permissionList)
      // 获取权限列表总数量
      const res2 = await getPermissionPage()
      this.total = res2
      console.log('获取权限列表总数量', res2)
    },
    // 分页功能
    // currentChange(page) {
    //   // console.log(page)
    //   this.page = page
    //   this.pagination._start = (page - 1) * this.pagination._limit
    //   this.loadList()
    // },
    // 添加权限
    addPermissionList(pid, type) {
      this.formData.pid = pid + ''
      this.formData.type = type
      // console.log('点击了添加权限')
      // 显示弹窗
      this.isShowDialog = true
    },
    // 弹窗确认按钮
    async btnOK() {
      if (this.formData.id) {
        // 编辑
        await editPermission(this.formData)
      } else {
        // 添加
        await addPermission(this.formData)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowDialog = false
      // 刷新页面
      this.loadList()
    },
    // 弹窗取消按钮
    btnCancel() {
      // 清理数据
      this.formData = {
        title: '',
        code: '',
        description: ''
      }
      // 关闭弹窗
      this.isShowDialog = false
    },
    // 删除功能
    async delPermissionList(id) {
      // console.log(id)
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?')
        // 调s接口
        await delPermission(id)
        // 提示用户
        this.$message.success('删除成功')
        // 刷新列表
        this.loadList()
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑数据回填
    async editPermissionList(id) {
      const res = await getPermissionId(id)
      console.log(res)
      this.formData = res
      // 显示弹窗
      this.isShowDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.card {
  display: flex;
  justify-content: end;
}

.page {
  padding: 20px;
  text-align: center;
}
</style>
